﻿using System.Text;

namespace Duo1JHttpServer.Framework.Utils
{
    /// <summary>
    /// html字符串构造器
    /// </summary>
    public class HtmlFormatter
    {
        private StringBuilder res = new StringBuilder();

        /// <summary>
        /// 添加<html>
        /// </summary>
        public HtmlFormatter AddHtmlTagStart()
        {
            res.Append("<html>");
            return this;
        }

        /// <summary>
        /// 添加</html>
        /// </summary>
        public HtmlFormatter AddHtmlTagEnd()
        {
            res.Append("</html>");
            return this;
        }

        /// <summary>
        /// 添加<head>
        /// </summary>
        public HtmlFormatter AddHeadTagStart()
        {
            res.Append("<head>");
            return this;
        }

        /// <summary>
        /// 添加</head>
        /// </summary>
        public HtmlFormatter AddHeadTagEnd()
        {
            res.Append("</head>");
            return this;
        }

        /// <summary>
        /// 添加<body>
        /// </summary>
        public HtmlFormatter AddBodyTagStart()
        {
            res.Append("<body>");
            return this;
        }

        /// <summary>
        /// 添加</body>
        /// </summary>
        public HtmlFormatter AddBodyTagEnd()
        {
            res.Append("</body>");
            return this;
        }

        /// <summary>
        /// 添加任意标签起始
        /// </summary>
        public HtmlFormatter AddTagStart(string tag)
        {
            res.Append($"<{tag}>");
            return this;
        }

        /// <summary>
        /// 添加任意标签结束
        /// </summary>
        public HtmlFormatter AddTagEnd(string tag)
        {
            res.Append($"</{tag}>");
            return this;
        }

        /// <summary>
        /// 添加字符串
        /// </summary>
        public HtmlFormatter AddInnerHtml(string str)
        {
            res.Append(str);
            return this;
        }

        /// <summary>
        /// 添加一行字符串，结尾换行
        /// </summary>
        public HtmlFormatter AddStrLine(string str)
        {
            res.Append($"{str}\n");
            return this;
        }

        /// <summary>
        /// 添加一行html
        /// </summary>
        public HtmlFormatter AddLineHtml(string tagName, string inner)
        {
            res.Append($"<{tagName}>{inner}</{tagName}>");
            return this;
        }

        /// <summary>
        /// 获取结果
        /// </summary>
        public string GetHtmlResult()
        {
            return res.ToString();
        }

        /// <summary>
        /// 获取结果
        /// </summary>
        public override string ToString()
        {
            return res.ToString();
        }
    }
}
